package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Transactional
    @Override
    public void add(Permission permission) {
        log.info("[权限-添加]data：{}", permission);
        permissionDao.insert(permission);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[权限-分页查询]data：{}", queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询数据库
        Page<Permission> page = permissionDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void delete(int id) {
        log.info("[权限-根据id删除]id：{}", id);
        permissionDao.deleteById(id);
    }

    @Transactional
    @Override
    public void edit(Permission permission) {
        log.info("[权限-编辑]data:{}", permission);
        permissionDao.update(permission);
    }


    @Override
    public Permission findById(Integer id) {
        log.info("[权限-根据ID查询]id:{}", id);
        return permissionDao.selectById(id);
    }

    @Override
    public List<Permission> findAll() {
        log.info("[权限-查询所有]~");
        // 调用DAO查询
        return permissionDao.selectAll();
    }

        @Override
        public List<Integer> findPermissionIdsByRoleid (Integer id){

            return permissionDao.findPermissionIdsByRoleid(id);
        }

}
